package doc2db.interfaces.action.open;

import doc2db.interfaces.action.AbstractGUIAction;
import doc2db.interfaces.action.Doc2DbDirAction;
import doc2db.interfaces.mainWindow.MyFrame;
import doc2db.interfaces.mainWindow.MyJEditorPane;
import doc2db.interfaces.mainWindow.MyPanel;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

/**
 *
 * @author yura
 */
public class OpenDir  extends AbstractGUIAction {//implements ActionListener {
    private MyJEditorPane medit ;
    private MEditPane edit ;
    /**главная панель приложения*/
    private MyPanel pane ;
    private MyFrame frame;

    public OpenDir(MyFrame frame, MyPanel pane) {
        this.frame = frame;
        this.pane = pane;
        this.medit = pane.textWeekendInformation;
        edit = new MEditPane(frame, pane);
    }

    @Override
    public boolean end(){
        pane.file.setText("");
        return true;
    }

    @Override public void action(ActionEvent arg0) {
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle(frame.getTitle() + ":Открыть папку");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);// disable the "All files" option.

        try {
            chooser.setCurrentDirectory(new File(((MEditPane) edit).getFileName()));
        } catch (Exception e) {
            chooser.setCurrentDirectory(new File("."));
        }

        try {
            int choice = chooser.showOpenDialog(frame);
            if (choice == JFileChooser.APPROVE_OPTION) {
                openDitr(chooser.getSelectedFile());
            }
        } catch (HeadlessException ex) {
            showError();
        }
    }

    private void showError() throws HeadlessException {
        String msg = "fatal error: save data and reload programm";
        int tp = JOptionPane.ERROR_MESSAGE;
        JOptionPane.showMessageDialog(null, msg, frame.getTitle(), tp);
    }

    private void openDitr(File file) throws HeadlessException {
        if (file.exists()) {
            try {
                edit.open(file.getAbsolutePath());
                edit.setAction(getEditAction());
                JScrollPane s = medit.getRoot();
                s.setViewportView(edit);
            } catch (Exception e) {
                System.err.println(e);
            }
        } else {
            JOptionPane.showMessageDialog(frame, "Выбранный каталог не существует");
        }
    }

    private Doc2DbDirAction getEditAction() {
        return new Doc2DbDirAction(frame, pane.file, pane.fname,
                pane.textDBSelects, pane.textWeekendInformation) {

            @Override
            public void actionPerformed(ActionEvent e) {
                String sf = getFirstDocFile(new File(edit.getFileName()));
                pane.file.setText("" + sf);
                super.actionPerformed(e);
            }
        };
    }

    public String getFirstDocFile(File f){
        if (f.isFile())
            return null;

        for (File file : f.listFiles()) {
            if (file.isFile() && file.getName().endsWith(".doc")){
                return file.getAbsolutePath();
            }
        }
        File[] arrF = f.listFiles();
        if (arrF.length > 0){
            return arrF[0].getAbsolutePath();
        }

        System.out.println("now doc-file at:\"" + f.getAbsolutePath() + "\"");
        return null;
    }
}
